/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package handlers.itemhandlers;

import net.sf.l2j.gameserver.datatables.ForgottenScrollsTable;
import net.sf.l2j.gameserver.datatables.SkillTable;
import net.sf.l2j.gameserver.handler.IItemHandler;
import net.sf.l2j.gameserver.model.L2ItemInstance;
import net.sf.l2j.gameserver.model.L2Skill;
import net.sf.l2j.gameserver.model.actor.L2Playable;
import net.sf.l2j.gameserver.model.actor.instance.L2PcInstance;
import net.sf.l2j.gameserver.network.SystemMessageId;
import net.sf.l2j.gameserver.network.serverpackets.SystemMessage;

/**
 * @author Flamingo
 */
public class ForgottenScroll implements IItemHandler
{
	/**
	 * 
	 * @see net.sf.l2j.gameserver.handler.IItemHandler#useItem(net.sf.l2j.gameserver.model.actor.instance.L2Playable, net.sf.l2j.gameserver.model.L2ItemInstance)
	 */
	public void useItem(L2Playable playable, L2ItemInstance item)
	{
		if(!(playable instanceof L2PcInstance)) return;
        int ID = item.getItemId();
        L2PcInstance player = (L2PcInstance)playable;
        if(!ForgottenScrollsTable.getInstance().validatePlayer(player, ID)){
            //TODO check retail Message
            SystemMessage sm = new SystemMessage(SystemMessageId.S1_CANNOT_BE_USED);
            sm.addItemName(item);
            player.sendPacket(sm);
            return;
        }
        //let's get the skill-ids related to this Forgotten Scroll
        int[] skillIDs = ForgottenScrollsTable.getInstance().getSkillIds(ID);
        if(player.destroyItem("ForgottenScroll", item, 1, player, true)){
            for(int i : skillIDs){
                L2Skill skill = SkillTable.getInstance().getInfo(i, 1);
                player.addSkill(skill, true);
            }
            player.sendSkillList();
        }
		return;
	}
	
	public int[] getItemIds()
	{
		return ForgottenScrollsTable.getInstance().getScrollIDS();
	}
}
